/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.material;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.utils.StringUtils;
import com.server.common.web.BaseController;
import com.server.modules.sys.entity.material.MaterialUpload;
import com.server.modules.sys.entity.member.Member;
import com.server.modules.sys.service.material.MaterialUploadService;
import com.server.modules.sys.service.member.MemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 资料上传管理Controller
 * @author 王磊
 * @version 2016-08-23
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/material/materialUpload")
public class MaterialUploadController extends BaseController {

	@Autowired
	private MaterialUploadService materialUploadService;
	
	@Autowired
	private MemberService memberService;
	

	@ModelAttribute
	public MaterialUpload get(@RequestParam(required=false) String id) {
		MaterialUpload entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = materialUploadService.get(id);
		}
		if (entity == null){
			entity = new MaterialUpload();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:material:materialUpload:view")
	@RequestMapping(value = {"list", ""})
	public String list(MaterialUpload materialUpload, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MaterialUpload> page = materialUploadService.findPage(new Page<MaterialUpload>(request, response), materialUpload); 
		model.addAttribute("page", page);
		model.addAttribute("materialUpload", materialUpload);
		return "modules/sys/material/materialUploadList";
	}

	@RequiresPermissions("sys:material:materialUpload:view")
	@RequestMapping(value = "form")
	public String form(MaterialUpload materialUpload, Model model) {
		model.addAttribute("materialUpload", materialUpload);
		return "modules/sys/material/materialUploadForm";
	}

	@RequiresPermissions("sys:material:materialUpload:edit")
	@RequestMapping(value = "save")
	public String save(MaterialUpload materialUpload, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, materialUpload)){
			return form(materialUpload, model);
		}
		materialUploadService.save(materialUpload);
		addMessage(redirectAttributes, "保存资料成功");
		return "redirect:"+Global.getAdminPath()+"/sys/material/materialUpload/?repage";
	}
	
	@RequiresPermissions("sys:material:materialUpload:edit")
	@RequestMapping(value = "delete")
	public String delete(MaterialUpload materialUpload, RedirectAttributes redirectAttributes) {
		materialUploadService.delete(materialUpload);
		addMessage(redirectAttributes, "删除资料成功");
		return "redirect:"+Global.getAdminPath()+"/sys/material/materialUpload/?repage";
	}
	
	/**
	 * 
	* @Title: checkPagee 
	* @Description: 上传资料清晰度审核页面
	* @author dingGuangbiao 314667996@qq.com
	* @date 2016-10-19 上午9:42:53 
	* @param @param member
	* @param @param request
	* @param @param response
	* @param @param model
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value = {"checkPage"})
	public String checkPagee(Member member, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Member> page = memberService.findPage(new Page<Member>(request, response), member); 
		model.addAttribute("page", page);
		model.addAttribute("member", member);
		return "modules/sys/material/checkPage";
	}
	
	@RequiresPermissions("sys:member:member:view")
	@RequestMapping(value = "checkFormPage")
	public String form(Member member, Model model) {
		String memberId = member.getId();
		MaterialUpload materialUpload=new MaterialUpload();
		materialUpload.setMemberId(memberId);
		List<MaterialUpload> findList = materialUploadService.findList(materialUpload);
		List<MaterialUpload> validList =new ArrayList<>();
		for (MaterialUpload upload : findList) {
			if(!upload.getTypeId().equals("1")){
				validList.add(upload);
			}
		}
		model.addAttribute("muList", validList);
		member = memberService.get(member.getId());
		model.addAttribute("member", member);
		return "modules/sys/material/checkFormPage";
	}
	@RequiresPermissions("sys:loan:customPay:docheckmaterialclear")
	@RequestMapping(value = "doCheck")
	@ResponseBody
	public Object doCheck(MaterialUpload obj, Model model) {
		if(obj==null||StringUtils.isBlank(obj.getId())||obj.getIsClear()==null){
			return false;
		}
		 return  materialUploadService.doCheck(obj);
	}
	@RequiresPermissions("sys:member:member:view")
	@RequestMapping(value = "doCheckBatch")
	@ResponseBody
	public Object doCheckBatch(Member member,Boolean isClear, Model model) {
		if(member==null||StringUtils.isBlank(member.getId())||isClear==null){
			return false;
		}
		 return  materialUploadService.doCheckBatch(member,isClear);
	}
	

}